var prev_id = getURLParam("prev_id"); 

function CheckForm() {
   alert("Submitting the form.");
}

function ClearForm() {
   alert("Clearing the form content.");
}

function page_init(){

    if (prev_id != null) {
        var txtPrevid = document.getElementById('txtPREV_ID');
        txtPrevid.value = prev_id;
    }

    loadAD();
    
    // CHAIN OF EVENT ON PAGE LOAD
    // Load the AD list
    // Select the adherent AD
    // Load the section list for the adherent ad
    // Load the journal state
    
    // On event when AD is changed
    // Load the Section list
}

function loadAD() {
    asynchHandler.init();
    asynchHandler.setEventSink(loadADCallBack);
    asynchHandler.sendHttpRequest('GET','GetADArray.aspx');
}

function loadADCallBack (){
    var AD = asynchHandler.receivedData;
    var ADArray = eval('(' + AD + ')');
    clearSelectOptions('listASSO');
    loadSelectOptions('listASSO', ADArray);  
    
    if ( prev_id != "" ) {        
        result = loadAdhAD(prev_id);
    }
}

function loadSection() {
    asynchHandler.init();
    asynchHandler.setEventSink(loadSectionCallBack);
    var o = document.getElementById('listASSO');
    var url = 'GetSectionByAD.aspx?AD=' + o.options[o.selectedIndex].value;
    asynchHandler.sendHttpRequest('GET',url);
}

function loadSectionCallBack(){
    var section = asynchHandler.receivedData;
    var sectionArray = eval('(' + section + ')');
    clearSelectOptions('listSECTION');
    loadSelectOptions('listSECTION', sectionArray);

    if (prev_id != null){
        loadRoutage(prev_id);
    }
}

function loadAdhAD(prev_id) {
    asynchHandler.setEventSink(loadAdhADCallBack);
    var url = 'GetADByPRevId.aspx?prev_id=' + prev_id;
    asynchHandler.sendHttpRequest('GET', url );
}

function loadAdhADCallBack(){
    var AdhAD = asynchHandler.receivedData;
    // Find the index value of our AdhAD
    var i = findSelectOptionIndex('listASSO', AdhAD);
    setSelectedOption('listASSO',i);
    loadSection();
}

function loadAdhSECTION(prev_id) {
    asynchHandler.setEventSink(loadAdhSECTIONCallBack);
    var url = 'GetSectionByPRevId.aspx?prev_id=' + prev_id;
    asynchHandler.sendHttpRequest('GET', url );
}

function loadAdhSECTIONCallBack(){
    var AdhSECTION = asynchHandler.receivedData;
    // Find the index value of our AdhAD
    var i = findSelectOptionIndex('listSECTION', AdhSECTION);
    setSelectedOption('listSECTION',i);
    loadSection();
}

function switchRoutage(){
    // First we ensure we have a prev_id. If not get back.
    
    if (prev_id.length > 0 && prev_id != null ){
        var chkRoutage = document.getElementById('chkROUTAGE');
        if (chkRoutage.checked==true){
            switchRoutage_Set(prev_id);
        }
        else {
            switchRoutage_Reset(prev_id);
        }
    }
}

function switchRoutage_Set(prev_id){
    // Make asynch call to switch routage
    asynchHandler.setEventSink(switchRoutage_Set_CallBack);
    var url = 'RoutageSetAdhByPrevId.aspx?prev_id=' + prev_id;
    asynchHandler.sendHttpRequest('GET', url );
}

function switchRoutage_Set_CallBack(){
    var result = asynchHandler.receivedData;
    if (result != "SUCCESS"){
        alert("Attention, la sauvegarde du routage n'a pas pu etre faite...");
        var chkRoutage = document.getElementById('chkROUTAGE');
        chkRoutage.checked=false;
    }
}

function switchRoutage_Reset(prev_id){
    // Make asynch call to switch routage
    asynchHandler.setEventSink(switchRoutage_Reset_CallBack); //Do we need to handle the call back? Yes, for errors.
    var url = 'RoutageResetAdhByPrevId.aspx?prev_id=' + prev_id;
    asynchHandler.sendHttpRequest('GET', url );
}

function switchRoutage_Reset_CallBack(){
    var result = asynchHandler.receivedData;
    if (result != "SUCCESS"){
        alert("Attention, la sauvegarde du routage n'a pas pu etre faite...");
        var chkRoutage = document.getElementById('chkROUTAGE');
        chkRoutage.checked=true;
    }
}

function loadRoutage(prev_id){
    asynchHandler.setEventSink(loadRoutage_CallBack);
    var url = 'RoutageGetAdhByPrevId.aspx?prev_id=' + prev_id;
    asynchHandler.sendHttpRequest('GET', url );
}

function loadRoutage_CallBack(){
    var result = asynchHandler.receivedData;
    
    var chkRoutage = document.getElementById('chkROUTAGE');
    if (result == "0" ) {
        chkRoutage.checked=false;
    }
    else if (result = "1") {
        chkRoutage.checked = true;
    }   
}

function isPrevIdFromURLParam(){
    var prevIdFromURLParam = false;
    if ( prev_id == "" || prev_id == null){
        false;
    }
    else {
        true;
    }
    return prevIdFromURLParam;
}

function createRoutage(){
    if (isPrevIdFromURLParam == false){
        var d = document.getElementById("txtPREV_ID");
        var newPrev_id = d.value;
        
        if (newPrev_id != null && newPrev_id != ""){
            asynchHandler.setEventSink(createRoutage_CallBack);
            var url = 'RoutageCreateByPrevId.aspx?prev_id=' + prev_id;
            asynchHandler.sendHttpRequest('GET', url );
        }
    }
}

function createRoutage_CallBack(){
    var result = asynchHandler.receivedData;
    if (result != "SUCCESS"){
        alert("Attention, la creation du routage n'a pas pu etre faite...");
    }
}